package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @ClassName: ShoppingCartController
 * @Author: xmc
 * @Date: 2022/8/19 20:15
 * @Version: 1.0
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     *
     *方法说明:查看购物车
     * @author xmc
     * @return com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.entity.ShoppingCart>>
     * @date 2022/8/20 7:07
     */

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentThreadLocal());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        return R.success(shoppingCartList);
    }

    @DeleteMapping("clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentThreadLocal());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //先查询数量
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()).or().eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        List<ShoppingCart> carts = shoppingCartService.list(wrapper);
        for (ShoppingCart cart : carts) {
            Integer number = cart.getNumber();
            if(number > 1 && cart.getDishId() != null){
                cart.setNumber(number -1);
                shoppingCartService.updateById(cart);
                return R.success("清理成功");
            }else if(number > 1 && cart.getSetmealId() != null){
                cart.setNumber(number -1);
                shoppingCartService.updateById(cart);
                return R.success("清理成功");
            } else
                break;
        }
        shoppingCartService.remove(wrapper);
        return R.success("清理成功");
    }



    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //设置用户id,指定是哪个用户的购物车数据
        Long userId = BaseContext.getCurrentThreadLocal();
        shoppingCart.setUserId(userId);
        //查询当前菜品或者套餐是否存在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        if(dishId != null){
            //添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            //添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);
        if(cartServiceOne != null){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }
}
